// protobuf数据编译解析模块

const ProtoBufJs = require('protobufjs')
const path = require('path')
const l = console.log
const fs = require('fs')

const data = {
    "id": "dbdfe4fa-288a-4304-a584-849fad3f989c",
    "tagid": "44",
    "siteset_id": 2,
    "ad_infos": [
        {
            "ad_owner_id": "1234",
            "ad_ids": [
                "1742176",
                "1762674"
            ]
        }
    ]
}


function proto(fp, regester){
	const PathToProto = path.resolve(process.cwd(), fp)
	const DesProto = ProtoBufJs.loadSync(PathToProto)
	regester.map(v=>{ this[v] = DesProto.lookup(v) })
}

proto.prototype.encode = function(data, obj){
	l(data)
	return this[obj].encode(data).finish()
}
proto.prototype.decode = function(data, obj){
	return this[obj].decode(data)
}

// let protobuf = new proto("./bin/meitu.proto",['Request'])
// let en = protobuf.encode(data, 'Request')
// let de = protobuf.decode(en, 'Request')
// l(en)
// l(de)



let protobuf = new proto("./bin/test.proto",['Person'])
let en = protobuf.encode({Name:'jk',Age:5,From:'1',idS:["54",'12'],Addr:[{name:'df',pinyin:'sdf',address:'sdf'}]}, 'Person')
let de = protobuf.decode(en, 'Person')
l(en)
l(de)


